diff options
| author | Mike Buland <mike@xagasoft.com> | 2012-08-04 18:34:57 -0600 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2012-08-04 18:34:57 -0600 |
| commit | 2406848173c445a94a1710106116ad796a8bacb9 (patch) | |
| tree | 7852d9e401d6c2833ee01c4a1c71f4fcc70a1362 | |
| parent | aaa733d789612289396d69bb6cf3a0e5de1798a8 (diff) | |
| download | libgenetic-2406848173c445a94a1710106116ad796a8bacb9.tar.gz libgenetic-2406848173c445a94a1710106116ad796a8bacb9.tar.bz2 libgenetic-2406848173c445a94a1710106116ad796a8bacb9.tar.xz libgenetic-2406848173c445a94a1710106116ad796a8bacb9.zip | |
Made some changes to make serializing workable.
There's no official format yet, but that's coming, most likely.
| -rw-r--r-- | src/explicitsimulation.h | 3 | ||||
| -rw-r--r-- | src/phenotype.h | 4 | ||||
| -rw-r--r-- | src/phenotypebinary.cpp | 42 | ||||
| -rw-r--r-- | src/phenotypebinary.h | 8 |
4 files changed, 42 insertions, 15 deletions
diff --git a/src/explicitsimulation.h b/src/explicitsimulation.h index 7713c71..e71b489 100644 --- a/src/explicitsimulation.h +++ b/src/explicitsimulation.h | |||
| @@ -22,11 +22,14 @@ namespace Genetic | |||
| 22 | virtual ~ExplicitSimulation(); | 22 | virtual ~ExplicitSimulation(); |
| 23 | 23 | ||
| 24 | void timestep(); | 24 | void timestep(); |
| 25 | const Population &getPopulation() const { return xPop; } | ||
| 25 | 26 | ||
| 26 | Genetic::PhenotypeId selectWeighted(); | 27 | Genetic::PhenotypeId selectWeighted(); |
| 27 | 28 | ||
| 28 | double getMinFitness() const { return dMinFitness; } | 29 | double getMinFitness() const { return dMinFitness; } |
| 29 | double getMaxFitness() const { return dMaxFitness; } | 30 | double getMaxFitness() const { return dMaxFitness; } |
| 31 | double getFitness( Genetic::PhenotypeId id ) const | ||
| 32 | { return hFitness.get( id ); } | ||
| 30 | 33 | ||
| 31 | protected: | 34 | protected: |
| 32 | void updateFitness(); | 35 | void updateFitness(); |
diff --git a/src/phenotype.h b/src/phenotype.h index 0eafb78..27173e2 100644 --- a/src/phenotype.h +++ b/src/phenotype.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #ifndef GENETIC_PHENOTYPE_H | 1 | #ifndef GENETIC_PHENOTYPE_H |
| 2 | #define GENETIC_PHENOTYPE_H | 2 | #define GENETIC_PHENOTYPE_H |
| 3 | 3 | ||
| 4 | #include <bu/stream.h> | ||
| 4 | #include <bu/string.h> | 5 | #include <bu/string.h> |
| 5 | #include <bu/hash.h> | 6 | #include <bu/hash.h> |
| 6 | #include <bu/variant.h> | 7 | #include <bu/variant.h> |
| @@ -33,6 +34,9 @@ namespace Genetic | |||
| 33 | 34 | ||
| 34 | virtual Bu::String toString()=0; | 35 | virtual Bu::String toString()=0; |
| 35 | 36 | ||
| 37 | virtual void write( Bu::Stream &rStream ) {}; | ||
| 38 | virtual void read( Bu::Stream &rStream ) {}; | ||
| 39 | |||
| 36 | bool hasProperty( const Bu::String &sKey ) const; | 40 | bool hasProperty( const Bu::String &sKey ) const; |
| 37 | Bu::Variant getProperty( const Bu::String &sKey ) const; | 41 | Bu::Variant getProperty( const Bu::String &sKey ) const; |
| 38 | void setProperty( const Bu::String &sKey, Bu::Variant vValue ); | 42 | void setProperty( const Bu::String &sKey, Bu::Variant vValue ); |
diff --git a/src/phenotypebinary.cpp b/src/phenotypebinary.cpp index 9d9e26c..8ecd394 100644 --- a/src/phenotypebinary.cpp +++ b/src/phenotypebinary.cpp | |||
| @@ -155,6 +155,38 @@ Bu::String Genetic::PhenotypeBinary::toString() | |||
| 155 | return sRet; | 155 | return sRet; |
| 156 | } | 156 | } |
| 157 | 157 | ||
| 158 | void Genetic::PhenotypeBinary::write( Bu::Stream &rStream ) | ||
| 159 | { | ||
| 160 | uint32_t uSize = htobe32( iSize ); | ||
| 161 | rStream.write( &uSize, 4 ); | ||
| 162 | uSize = (iSize/8) + ((iSize%8)?1:0); | ||
| 163 | for( int j = 0; j < uSize; j++ ) | ||
| 164 | { | ||
| 165 | rStream.write( | ||
| 166 | &((char *)&aGenes[j/sizeof(uint_fast32_t)])[j%sizeof(uint_fast32_t)], | ||
| 167 | 1 | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | void Genetic::PhenotypeBinary::read( Bu::Stream &rStream ) | ||
| 173 | { | ||
| 174 | uint32_t uSize; | ||
| 175 | rStream.read( &uSize, 4 ); | ||
| 176 | iSize = be32toh( uSize ); | ||
| 177 | iWords = wordsForBits(iSize); | ||
| 178 | delete[] aGenes; | ||
| 179 | aGenes = new uint_fast32_t[iWords]; | ||
| 180 | uSize = (iSize/8) + ((iSize%8)?1:0); | ||
| 181 | for( int j = 0; j < uSize; j++ ) | ||
| 182 | { | ||
| 183 | rStream.read( | ||
| 184 | &((char *)&aGenes[j/sizeof(uint_fast32_t)])[j%sizeof(uint_fast32_t)], | ||
| 185 | 1 | ||
| 186 | ); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 158 | void Genetic::PhenotypeBinary::extractBits( uint32_t &rTarget, int iStart, | 190 | void Genetic::PhenotypeBinary::extractBits( uint32_t &rTarget, int iStart, |
| 159 | int iBits ) | 191 | int iBits ) |
| 160 | { | 192 | { |
| @@ -189,13 +221,3 @@ void Genetic::PhenotypeBinary::extractBits( uint64_t &rTarget, int iStart, | |||
| 189 | } | 221 | } |
| 190 | } | 222 | } |
| 191 | 223 | ||
| 192 | void Genetic::PhenotypeBinary::write( Bu::Stream &rStream ) | ||
| 193 | { | ||
| 194 | rStream.write( aGenes, sizeof(uint_fast32_t)*iWords ); | ||
| 195 | } | ||
| 196 | |||
| 197 | void Genetic::PhenotypeBinary::read( Bu::Stream &rStream ) | ||
| 198 | { | ||
| 199 | rStream.read( aGenes, sizeof(uint_fast32_t)*iWords ); | ||
| 200 | } | ||
| 201 | |||
diff --git a/src/phenotypebinary.h b/src/phenotypebinary.h index f312187..92146d2 100644 --- a/src/phenotypebinary.h +++ b/src/phenotypebinary.h | |||
| @@ -3,8 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | #include "genetic/phenotype.h" | 4 | #include "genetic/phenotype.h" |
| 5 | 5 | ||
| 6 | #include <bu/stream.h> | ||
| 7 | |||
| 8 | namespace Genetic | 6 | namespace Genetic |
| 9 | { | 7 | { |
| 10 | class PhenotypeBinary : public Phenotype | 8 | class PhenotypeBinary : public Phenotype |
| @@ -21,12 +19,12 @@ namespace Genetic | |||
| 21 | int iCount, int iDest=-1 ); | 19 | int iCount, int iDest=-1 ); |
| 22 | virtual Bu::String toString(); | 20 | virtual Bu::String toString(); |
| 23 | 21 | ||
| 22 | virtual void write( Bu::Stream &rStream ); | ||
| 23 | virtual void read( Bu::Stream &rStream ); | ||
| 24 | |||
| 24 | void extractBits( uint32_t &rTarget, int iStart, int iBits ); | 25 | void extractBits( uint32_t &rTarget, int iStart, int iBits ); |
| 25 | void extractBits( uint64_t &rTarget, int iStart, int iBits ); | 26 | void extractBits( uint64_t &rTarget, int iStart, int iBits ); |
| 26 | 27 | ||
| 27 | void write( Bu::Stream &rStream ); | ||
| 28 | void read( Bu::Stream &rStream ); | ||
| 29 | |||
| 30 | private: | 28 | private: |
| 31 | int iSize; | 29 | int iSize; |
| 32 | int iWords; | 30 | int iWords; |
